<?php
/**
 * User: b179a9
 * Date: 9/20/11
 * Time: 5:52 PM
 */

/**
 * Admin for static pages.
 */
class PagesController extends Zend_Controller_Action
{

    /**
     * @var Lib_Flash
     */
    protected $_flashMessenger = null;

    function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout->disableLayout();
        } else {
            $this->_helper->layout()->setLayout('admin-layout');
        }
        $this->_flashMessenger = new Lib_Flash();
    }

    function indexAction()
    {
        $pagesManager = new \Managers\Pages();
        $pages = $pagesManager->getList(null, true);

        $this->view->pages = $pages;
    }

    function editAction()
    {

        $id = $this->_request->getParam('id');
        $pagesManager = new \Managers\Pages();
        $page = $pagesManager->getById($id);

        $form = new App_Form_Page_Form();
        $form->populate($page);

        if (!$page) {
            $this->_flashMessenger->addMessage('Such page does not exist.', Lib_Flash::ERROR, Lib_Flash::PRE);
            $this->_redirect('/pages');
        }

        $params = $this->_request->getParams();

        if ($this->_request->isPost()) {
            if ($form->isValid($params)) {
                try {
                    $pagesManager->save($params);
                    $this->_flashMessenger->addMessage('Page successfully updated.', Lib_Flash::NOTICE, Lib_Flash::PRE);
                    $this->_redirect('/pages');
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::POST);
                }
            } else {
                $this->_flashMessenger->addMessage('Please, check input data.', Lib_Flash::ERROR, Lib_Flash::POST);
            }
        }
        $this->view->id = $page['id'];
        $this->view->form = $form->render();
        $this->renderScript('/pages/form.phtml');
    }

    function addAction()
    {
        $form = new App_Form_Page_Form();

        $params = $this->_request->getParams();
        if ($this->_request->isPost()) {
            if ($form->isValid($params)) {
                $pagesManager = new \Managers\Pages();
                try {
                    $pagesManager->save($params);
                    $this->_flashMessenger->addMessage('Page successfully added.', Lib_Flash::NOTICE, Lib_Flash::PRE);
                    $this->_redirect('/pages');
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::POST);
                }
            } else {
                $this->_flashMessenger->addMessage('Please, check input data.', Lib_Flash::ERROR, Lib_Flash::POST);
            }
        }

        $this->view->form = $form->render();
        $this->renderScript('/pages/form.phtml');
    }

    function deleteAction()
    {
        $id = $this->_request->getParam('id');

        $pagesManager = new \Managers\Pages();
        if($pagesManager->delete($id)) {
            $this->_flashMessenger->addMessage('Page successfully deleted.', Lib_Flash::NOTICE, Lib_Flash::PRE);
        } else {
            $this->_flashMessenger->addMessage('Page does not exist.', Lib_Flash::ERROR, Lib_Flash::PRE);
        }
        $this->_redirect('/pages');

    }

    function dndAction() {

        if(!$this->_request->isXmlHttpRequest()) {
            $this->_redirect('/pages');
        }

        if($this->_request->isPost()) {
            $operation = $this->_request->getParam('operation');
            if('move_node' == $operation) {
                $id = $this->_request->getParam('id');
                $ref = $this->_request->getParam('ref');
                $pagesManager = new \Managers\Pages();
                $result = $pagesManager->changeParent($id, $ref);

                $data = array('status' => $result, 'id' => $id);
                $this->view->data = $data;
                $this->renderScript('/ajax/json.phtml');
            }
        }

    }

}
